<?

#include_once("sogeneric.php");
include_once("soset.php");
include_once("sodebug.php");
include_once("sodebug.inc");
include_once("dzkone.php");
include_once("soinifiles.php");

include("sodebug.inc");

#$soDebugLevel = SOD_DET;
$soDebugLogFile = "/tmp/luteet.log";


/* Get initial values from ini file */
$ini = new soIniData("file://arevimport.ini");
$dbname = $ini->getValue("general", "database");
$lockfile = $ini->getValue("general", "lockfile");
$folder = $ini->getValue("file", "folder");
$failed_log = $ini->getValue("file", "failed_log");
$postfix = $ini->getValue("file", "extension");


/* check for lockfile and exit if it exists */
if (file_exists($lockfile)) die(); // <-- EXIT
exec("touch ".$lockfile);

$kone = new dzKone();
$kone->setDbAccess($dbname);
if ($failed_log != "") $ffp = fopen($failed_log, "a");

$dir = new soDir($folder, "file", "c");
$fcount = $dir->count();
for ($f = 0; $f < $fcount; $f++) {
    $name = $dir->getValue($f, "n");
    if (($postfix == "") || 
        (strtoupper(substr($name, strlen($name) -4)) == 
         strtoupper($postfix))) {
echo "$name\n";
        $error = 0;
        $streams = new soInputStream("file://$folder/$name");
        $streams = soExplode($streams->Data, "\n\n");
        foreach ($streams as $stream) {
            $data = soExplode($stream, "\n");
            $set = new soSet();
            $set->joinKeyValueArray($data);
            //$url = $set->encode();
            $url = $set->toString(1, "&", "=");

echo "menee\n";
            if (!$kone->_requestStr($url, "&")) {
                if ($failed_log != "") fwrite($ffp, $stream);
            }; // if error
        }; // foreach

        unlink("$folder/$name");
    };
};

if ($failed_log != "") fclose($ffp);
unlink($lockfile);

?>
